﻿using System;
using System.Linq;
using System.Web.Mvc;
using mBlog.Services;
using mBlog.ViewModels;
using mBlog.Extensions;

namespace mBlog.Controllers
{
    public class TagController : BaseController
    {

        private IPostService postService;
        private ITagService tagService;

        public TagController(IPostService postService, ITagService tagService)
        {
            this.postService = postService;
            this.tagService = tagService;
        }

        public ActionResult View(string urlName, int? page)
        {

            ListView<PostView> lvTag = new ListView<PostView>
                                       {
                                           Title = string.Format("标签：{0}", urlName),
                                           List = (from p in postService.GetByTag(urlName, page, ItemsPerPage)
                                                  select new PostView()
                                                             {
                                                                 Post = p.MapTo<PostDetail>(),
                                                                 Categories = p.Categories.MapTo<CategoryDetail>(),
                                                                 Tags = p.Tags.MapTo<TagDetail>()
                                                             }).ToList(),
                                           Pager = new PagerView()
                                                       {
                                                           ControllerName = "Tag",
                                                           ActionName = "View",
                                                           CurrentPage = page ?? 0,
                                                           PageCount =
                                                               (int)
                                                               Math.Ceiling(
                                                                   (double) tagService.GetPostTotalUnderTag(urlName)/
                                                                   ItemsPerPage)
                                                       }
                                       };



            return View(lvTag);
        }

        
    }
}
